package org.rsbot.script.wrappers;

import I;
import java.awt.Point;
import java.awt.Rectangle;
import org.rsbot.client.Client;
import org.rsbot.client.RSInterfaceNode;
import org.rsbot.script.internal.wrappers.HashTable;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.MethodContext;
import org.rsbot.script.methods.MethodProvider;
import org.rsbot.script.methods.Mouse;

public class RSComponent extends MethodProvider
{
  private final int index;
  private final RSInterface parInterface;
  private final RSComponent parent;

  RSComponent(MethodContext paramMethodContext, RSInterface paramRSInterface, int paramInt)
  {
    super(paramMethodContext);
    this.parInterface = paramRSInterface;
    this.index = paramInt;
    this.parent = null;
  }

  RSComponent(MethodContext paramMethodContext, RSInterface paramRSInterface, RSComponent paramRSComponent, int paramInt)
  {
    super(paramMethodContext);
    this.parInterface = paramRSInterface;
    this.parent = paramRSComponent;
    this.index = paramInt;
  }

  public boolean doAction(String paramString)
  {
    if (!isValid())
      return false;
    Rectangle localRectangle = getArea();
    if ((localRectangle.x == -1) || (localRectangle.y == -1) || (localRectangle.width == -1) || (localRectangle.height == -1)) {
      return false;
    }
    if (localRectangle.contains(this.methods.mouse.getLocation())) {
      return this.methods.menu.doAction(paramString);
    }

    int i = localRectangle.x + 1; int j = localRectangle.y + 1;
    int k = i + localRectangle.width - 2; int m = j + localRectangle.height - 2;

    this.methods.mouse.move(random(i, k, localRectangle.width / 3), random(j, m, localRectangle.height / 3));

    sleep(random(40, 80));
    return this.methods.menu.doAction(paramString);
  }

  public boolean doClick()
  {
    return doClick(true);
  }

  public boolean doClick(boolean paramBoolean)
  {
    if (!isValid()) {
      return false;
    }

    Rectangle localRectangle = getArea();
    if ((localRectangle.x == -1) || (localRectangle.y == -1) || (localRectangle.width == -1) || (localRectangle.height == -1)) {
      return false;
    }
    if (localRectangle.contains(this.methods.mouse.getLocation())) {
      this.methods.mouse.click(true);
      return true;
    }

    int i = localRectangle.x + 1; int j = localRectangle.y + 1;
    int k = i + localRectangle.width - 2; int m = j + localRectangle.height - 2;

    this.methods.mouse.click(random(i, k, localRectangle.width / 3), random(j, m, localRectangle.height / 3), paramBoolean);

    return true;
  }

  public boolean doHover()
  {
    if (!isValid()) {
      return false;
    }

    Rectangle localRectangle = getArea();
    if ((localRectangle.x == -1) || (localRectangle.y == -1) || (localRectangle.width == -1) || (localRectangle.height == -1)) {
      return false;
    }
    if (localRectangle.contains(this.methods.mouse.getLocation())) {
      return false;
    }

    int i = localRectangle.x + 1; int j = localRectangle.y + 1;
    int k = i + localRectangle.width - 2; int m = j + localRectangle.height - 2;

    this.methods.mouse.move(random(i, k, localRectangle.width / 3), random(j, m, localRectangle.height / 3));

    return true;
  }

  public boolean containsAction(String paramString)
  {
    for (String str : getActions()) {
      if (str.toLowerCase().contains(paramString.toLowerCase()))
        return true;
    }
    return false;
  }

  public boolean containsText(String paramString)
  {
    return getText().contains(paramString);
  }

  public int getAbsoluteX()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface == null) {
      return -1;
    }

    int i = 0;

    int j = getParentID();
    if (j != -1) {
      i = this.methods.interfaces.getComponent(j >> 16, j & 0xFFFF).getAbsoluteX();
    }
    else
    {
      Rectangle[] arrayOfRectangle = this.methods.client.getRSInterfaceBoundsArray();

      int k = localRSInterface.getBoundsArrayIndex();
      if ((k >= 0) && (arrayOfRectangle != null) && (k < arrayOfRectangle.length) && (arrayOfRectangle[k] != null)) {
        return arrayOfRectangle[k].x;
      }

      i = localRSInterface.getMasterX();
    }

    i += localRSInterface.getX();

    if (localRSInterface.getParentID() != -1) {
      localRSInterface = this.methods.interfaces.getComponent(localRSInterface.getParentID() >> 16, localRSInterface.getParentID() & 0xFFFF).getInterfaceInternal();
      if (localRSInterface.getHorizontalScrollBarSize() != 0) {
        i -= localRSInterface.getHorizontalScrollBarThumbPosition();
      }
    }

    return i;
  }

  public int getAbsoluteY()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface == null) {
      return -1;
    }

    int i = 0;

    int j = getParentID();
    if (j != -1) {
      i = this.methods.interfaces.getComponent(j >> 16, j & 0xFFFF).getAbsoluteY();
    }
    else
    {
      Rectangle[] arrayOfRectangle = this.methods.client.getRSInterfaceBoundsArray();

      int k = localRSInterface.getBoundsArrayIndex();
      if ((k >= 0) && (arrayOfRectangle != null) && (k < arrayOfRectangle.length) && (arrayOfRectangle[k] != null)) {
        return arrayOfRectangle[k].y;
      }

      i = localRSInterface.getMasterY();
    }

    i += localRSInterface.getY();

    if (localRSInterface.getParentID() != -1) {
      localRSInterface = this.methods.interfaces.getComponent(localRSInterface.getParentID() >> 16, localRSInterface.getParentID() & 0xFFFF).getInterfaceInternal();
      if (localRSInterface.getVerticalScrollBarSize() != 0) {
        i -= localRSInterface.getVerticalScrollBarPosition();
      }
    }

    return i;
  }

  public String[] getActions()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getActions();
    return new String[0];
  }

  public Rectangle getArea()
  {
    return new Rectangle(getAbsoluteX(), getAbsoluteY(), getWidth(), getHeight());
  }

  public int getBackgroundColor()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getTextureID();
    return -1;
  }

  public int getBorderThickness()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getBorderThickness();
    return -1;
  }

  public int getBoundsArrayIndex()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getBoundsArrayIndex();
    }
    return -1;
  }

  public RSComponent[] getComponents()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if ((localRSInterface != null) && (localRSInterface.getComponents() != null)) {
      RSComponent[] arrayOfRSComponent = new RSComponent[localRSInterface.getComponents().length];
      for (int i = 0; i < arrayOfRSComponent.length; i++) {
        arrayOfRSComponent[i] = new RSComponent(this.methods, this.parInterface, this, i);
      }
      return arrayOfRSComponent;
    }
    return new RSComponent[0];
  }

  public RSComponent getComponent(int paramInt)
  {
    RSComponent[] arrayOfRSComponent = getComponents();
    if ((paramInt >= 0) && (paramInt < arrayOfRSComponent.length)) {
      return arrayOfRSComponent[paramInt];
    }
    return null;
  }

  public int getComponentID()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getComponentID();
    return -1;
  }

  public int getComponentIndex()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getComponentIndex();
    }
    return -1;
  }

  public int getComponentStackSize()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getComponentStackSize();
    }
    return -1;
  }

  public String getComponentName()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getComponentName();
    }
    return "";
  }

  public int getHeight()
  {
    if (!isInScrollableArea()) {
      return getRealHeight();
    }
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getHeight() - 4;
    return -1;
  }

  public int getID() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getID();
    return -1;
  }

  public int getIndex()
  {
    return this.index;
  }

  public int getModelID()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getModelID();
    }
    return -1;
  }

  public int getModelType()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getModelType();
    }
    return -1;
  }

  public int getModelZoom() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getModelZoom();
    return -1;
  }

  public int getParentID()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface == null) {
      return -1;
    }
    if (localRSInterface.getParentID() != -1) {
      return localRSInterface.getParentID();
    }
    int i = getID() >>> 16;
    HashTable localHashTable = new HashTable(this.methods.client.getRSInterfaceNC());

    for (RSInterfaceNode localRSInterfaceNode = (RSInterfaceNode)localHashTable.getFirst(); localRSInterfaceNode != null; localRSInterfaceNode = (RSInterfaceNode)localHashTable.getNext()) {
      if (i == localRSInterfaceNode.getMainID()) {
        return (int)localRSInterfaceNode.getID();
      }
    }
    return -1;
  }

  public RSInterface getInterface()
  {
    return this.parInterface;
  }

  public RSComponent getParent()
  {
    return this.parent;
  }

  public Point getLocation()
  {
    return new Point(getAbsoluteX(), getAbsoluteY());
  }

  public Point getCenter()
  {
    return new Point(getAbsoluteX() + getWidth() / 2, getAbsoluteY() + getHeight() / 2);
  }

  public int getRelativeX()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getX();
    return -1;
  }

  public int getRelativeY()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getY();
    return -1;
  }

  public int getVerticalScrollPosition() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getVerticalScrollBarPosition();
    return -1;
  }

  public int getHorizontalScrollPosition() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getHorizontalScrollBarThumbPosition();
    return -1;
  }

  public int getScrollableContentHeight() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getVerticalScrollBarSize();
    return -1;
  }

  public int getScrollableContentWidth() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getHorizontalScrollBarSize();
    return -1;
  }

  public int getRealHeight() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getVerticalScrollBarThumbSize();
    return -1;
  }

  public int getRealWidth() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getHorizontalScrollBarThumbSize();
    return -1;
  }

  public boolean isInScrollableArea()
  {
    if (getParentID() == -1) {
      return false;
    }

    RSComponent localRSComponent = this.methods.interfaces.getComponent(getParentID());
    while ((localRSComponent.getScrollableContentHeight() == 0) && (localRSComponent.getParentID() != -1)) {
      localRSComponent = this.methods.interfaces.getComponent(localRSComponent.getParentID());
    }

    return localRSComponent.getScrollableContentHeight() != 0;
  }

  public String getSelectedActionName()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getSelectedActionName();
    return "";
  }

  public int getShadowColor() {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getShadowColor();
    return -1;
  }

  public int getSpecialType()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      return localRSInterface.getSpecialType();
    }
    return -1;
  }

  public String getSpellName()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getSpellName();
    return "";
  }

  public String getText()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getText();
    return "";
  }

  public int getTextColor()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getTextColor();
    return -1;
  }

  public String getTooltip()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getToolTip();
    return "";
  }

  public int getType()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getType();
    return -1;
  }

  public int[][] getValueIndexArray()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null) {
      int[][] arrayOfInt1 = localRSInterface.getValueIndexArray();
      if (arrayOfInt1 != null) {
        int[][] arrayOfInt2 = new int[arrayOfInt1.length][0];
        for (int i = 0; i < arrayOfInt1.length; i++) {
          int[] arrayOfInt = arrayOfInt1[i];
          if (arrayOfInt == null)
            continue;
          arrayOfInt2[i] = ((int[])arrayOfInt.clone());
        }

        return arrayOfInt2;
      }
    }
    return new int[0][0];
  }

  public int getWidth()
  {
    if (!isInScrollableArea()) {
      return getRealWidth();
    }
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getWidth() - 4;
    return -1;
  }

  public int getXRotation()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getXRotation();
    return -1;
  }

  public int getYRotation()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getYRotation();
    return -1;
  }

  public int getZRotation()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    if (localRSInterface != null)
      return localRSInterface.getZRotation();
    return -1;
  }

  public boolean isVerticallyFlipped()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    return (localRSInterface != null) && (localRSInterface.isVerticallyFlipped());
  }

  public boolean isHorizontallyFlipped()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    return (localRSInterface != null) && (localRSInterface.isHorizontallyFlipped());
  }

  public boolean isInventory()
  {
    org.rsbot.client.RSInterface localRSInterface = getInterfaceInternal();
    return (localRSInterface != null) && (localRSInterface.isInventoryRSInterface());
  }

  public boolean isValid()
  {
    return (this.parInterface.isValid()) && (getBoundsArrayIndex() != -1);
  }

  public boolean equals(Object paramObject)
  {
    if (paramObject == this)
      return true;
    if ((paramObject instanceof RSComponent)) {
      RSComponent localRSComponent = (RSComponent)paramObject;
      return (this.index == localRSComponent.index) && (localRSComponent.parInterface.equals(this.parInterface));
    }
    return false;
  }

  public int hashCode()
  {
    return this.parInterface.getIndex() * 31 + this.index;
  }

  org.rsbot.client.RSInterface getInterfaceInternal()
  {
    Object localObject;
    if (this.parent != null) {
      localObject = this.parent.getInterfaceInternal();
      if (localObject != null) {
        org.rsbot.client.RSInterface[] arrayOfRSInterface = ((org.rsbot.client.RSInterface)localObject).getComponents();
        if ((arrayOfRSInterface != null) && (this.index >= 0) && (this.index < arrayOfRSInterface.length))
          return arrayOfRSInterface[this.index];
      }
    }
    else {
      localObject = this.parInterface.getChildrenInternal();
      if ((localObject != null) && (this.index < localObject.length)) {
        return localObject[this.index];
      }
    }
    return (org.rsbot.client.RSInterface)null;
  }
}